package com.leet.code.lcof;

import java.util.*;

public class Code45 {
    public static void main(String[] args) {

//        int[] nums = {824,938,1399,5607,6973,5703,9609,4398,8247};
        int[] nums = {121,12};
        System.out.println(new Code45().minNumber(nums));
//        Long s1 = Long.parseLong("111111111");
//        Long s2 = Long.parseLong("222222222");
//        Long s3 = Long.parseLong("333333333");
//
//        System.out.println(s1.compareTo(s3));
//        System.out.println(s2.compareTo(s3));

    }

    public String minNumber(int[] nums) {
        Map<String, Integer> map = new HashMap<>();
        PriorityQueue<String> priorityQueue = new PriorityQueue<>();
        for (int i : nums) {
            StringBuilder k = new StringBuilder(String.valueOf(i));
            String s = k.substring(0, 1);
            String ll = k.substring(k.length() - 1);
            int sl = k.length();
            for (int l = 9 - sl; l > 0; l--) {
                k.append(s);
            }
            k.append(ll).append(sl);
            String kk = k.toString();
            map.put(kk, i);
            priorityQueue.add(kk);
        }

        StringBuilder sb = new StringBuilder();
        while (priorityQueue.size() != 0) {
            sb.append(map.get(priorityQueue.poll()));
        }

        return sb.toString();
    }


    public String minNumber1(int[] nums) {

        Map<String, List<String>> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            String s = String.valueOf(nums[i]);
            String a = s.substring(0, 1);

            List<String> ls = map.get(a);
            if (ls == null) {
                ls = new ArrayList<>();
            }
            inset(ls, s);
            map.put(a, ls);
        }
        StringBuilder sb = new StringBuilder();
        for (int j = 1; j <= 9; j++) {
            List<String> list = map.get(String.valueOf(j));
            if (list == null || list.size() == 0) {
                continue;
            }
            for (String s : list) {
                sb.append(s);
            }
        }

        return sb.toString();

    }


    public void inset(List<String> list, String s) {
        int l = s.length();
        String f = s.substring(0, 1);
        for (int i = 0; i < list.size(); i++) {
            String a = list.get(i);
        }

    }

    public String b(String f, String s, int i) {
        StringBuilder sBuilder = new StringBuilder(s);
        for (; i > 0; i--) {
            sBuilder.append(f);
        }
        return sBuilder.toString();
    }


}
